﻿using System; 
using System.Configuration; 
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace KPIS.GERP
{
    public static class SsMng
    {
        public static string CookieName = "UserDetail";
        public static string EssLoginUrl = "~/App_UI/Forms/Login.aspx";
        public static DateTime NextExpire = DateTime.Now.AddHours(8760);

        #region Features
        public static void SetLoginSession(string userSeq, string empSeq, string userName, string workingJobSiteSeq, string posSeq,
            string height, string tokenKey, string tokenUserTempData, string workingJobSiteName = "", string workAreaSeq = "0")
        {
            Set(SessionLogin.UserSeq, userSeq);
            Set(SessionLogin.EmpSeq, empSeq);
            Set(SessionLogin.UserName, userName);
            Set(SessionLogin.WorkingJobSiteSeq, workingJobSiteSeq);
            Set(SessionLogin.Height, height);
            Set(SessionLogin.TokenKey, tokenKey);
            Set(SessionLogin.TokenUserTempData, tokenUserTempData);
            Set(SessionLogin.WorkingJobSiteName, workingJobSiteName);
            Set(SessionLogin.WorkAreaSeq, workAreaSeq);
            Set(SessionLogin.PosSeq, posSeq);
        }
        public static string EmpSeq
        {
            get { return Get(SessionLogin.EmpSeq); }
            set { Set(SessionLogin.EmpSeq, value); }
        }

        public static string PosSeq
        {
            get { return Get(SessionLogin.PosSeq); }
            set { Set(SessionLogin.PosSeq, value); }
        }

        public static string UserName
        {
            get { return Get(SessionLogin.UserName); }
            set { Set(SessionLogin.UserName, value); }
        }

        public static string UserSeq
        {
            get { return Get(SessionLogin.UserSeq); }
            set { Set(SessionLogin.UserSeq, value); }
        }

        public static string WorkingJobSiteSeq
        {
            get { return Get(SessionLogin.WorkingJobSiteSeq); }
            set { Set(SessionLogin.WorkingJobSiteSeq, value); }
        }

        public static string WorkingJobSiteName
        {
            get { return Get(SessionLogin.WorkingJobSiteName); }
            set { Set(SessionLogin.WorkingJobSiteName, value); }
        }

        public static string WorkAreaSeq
        {
            get { return Get(SessionLogin.WorkAreaSeq); }
            set { Set(SessionLogin.WorkAreaSeq, value); }
        }

        public static string Height
        {
            get { return String.IsNullOrEmpty(Get(SessionLogin.Height)) ? "750" : Get(SessionLogin.Height); }
            set { Set(SessionLogin.Height, value); }
        }

        public static string TokenKey
        {
            get { return Get(SessionLogin.TokenKey); }
            set { Set(SessionLogin.TokenKey, value); }
        }

        public static string TokenUserTempData
        {
            get { return Get(SessionLogin.TokenUserTempData); }
            set { Set(SessionLogin.TokenUserTempData, value); }
        }

        public static int EmpSeqI
        {
            get { return ConvertInt(Get(SessionLogin.EmpSeq)); }
            set { Set(SessionLogin.EmpSeq, value.ToString()); }
        }

        public static int PosSeqI
        {
            get { return ConvertInt(Get(SessionLogin.PosSeq)); }
            set { Set(SessionLogin.PosSeq, value.ToString()); }
        }

        public static int UserSeqI
        {
            get { return ConvertInt(Get(SessionLogin.UserSeq)); }
            set { Set(SessionLogin.UserSeq, value.ToString()); }
        }

        public static int WorkingJobSiteSeqI
        {
            get { return ConvertInt(Get(SessionLogin.WorkingJobSiteSeq)); }
            set { Set(SessionLogin.WorkingJobSiteSeq, value.ToString()); }
        }


        public static int HeightI
        {
            get { return ConvertInt(Get(SessionLogin.Height)) == 0 ? 750 : ConvertInt(Get(SessionLogin.Height)); }
            set { Set(SessionLogin.Height, value.ToString()); }
        }

        public static Unit HeightU
        {
            get { return Unit.Pixel(HeightI == 0 ? 750 : HeightI); }
            set { Set(SessionLogin.Height, ((int)value.Value).ToString()); }
        }
         
        public static Unit GetHeightU(int minusPixel = 0, int minHeight = 100)
        {
            int height = HeightI == 0 ? 750 : HeightI;
            height = height - minusPixel;
            height = height >= minHeight ? height : minHeight;

            return Unit.Pixel(height);
        }

        public static void Logout()
        {
            HttpCookie cookie = GetCookieNotNull();
            cookie.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Session.Abandon();
        }

        public static void Redirect()
        {
            HttpContext.Current.Response.Redirect(EssLoginUrl);
        }

        public static bool IsLogin()
        {
            return !string.IsNullOrEmpty(Get(SessionLogin.EmpSeq, false)) && !string.IsNullOrEmpty(Get(SessionLogin.UserSeq, false));
        }

        public static void Set(SessionLogin key, string value)
        {
            Set(key.ToString(), value);
        }

        public static void Set(string key, string value)
        {
            SessionSet(key, value);
            CookieSet(key, value);
        }

        public static string Get(SessionLogin key, bool redirectIfNotLogin = true)
        {
            return Get(key.ToString(), redirectIfNotLogin);
        }

        public static string Get(string key, bool redirectIfNotLogin = true)
        {
            if (string.IsNullOrEmpty(SessionGet(key)))
            {
                SessionSet(key, CookieGet(key));
            }
            else if (string.IsNullOrEmpty(CookieGet(key)))
            {
                CookieSet(key, SessionGet(key));
            }
            if (redirectIfNotLogin && !IsLogin()) Redirect();

            return SessionGet(key);
        }

#endregion

        #region Base

        private static void SessionSet(string key, string value)
        {
            HttpContext.Current.Session[key] = value;
        }

        private static string SessionGet(string key)
        {
            return ConvertString(HttpContext.Current.Session[key]);
        }

        private static void CookieSet(string key, string value)
        {
            var cookie = GetCookieNotNull();
            cookie[key] = value;
        }

        private static string CookieGet(string key)
        {
            var cookie = GetCookieNotNull();
            return cookie[key];
        }

        private static HttpCookie GetCookieNotNull()
        {
            if (HttpContext.Current.Request.Cookies[CookieName] == null)
            {
                HttpContext.Current.Response.Cookies.Set(new HttpCookie(CookieName));
            }

            var cookie = HttpContext.Current.Response.Cookies[CookieName];
            cookie.Expires = NextExpire;
            return cookie;
        }


        public new static string ToString()
        {
            StringBuilder result = new StringBuilder();
            result.Append("This is session<br>");
            foreach (var s in HttpContext.Current.Session.Keys)
            {
                result.Append(s);
                result.Append("=");
                result.Append(HttpContext.Current.Session[s.ToString()]);
                result.Append("<br>");
            }
            result.Append("<br>");
            result.Append("This is cookie<br>");
            HttpCookie ck = HttpContext.Current.Request.Cookies[CookieName];
            if (ck != null && ck.HasKeys)
            {
                foreach (var s in ck.Values)
                {
                    if (s != null)
                    {
                        result.Append(s);
                        result.Append("=");
                        result.Append(ck[s.ToString()]);
                        result.Append("<br>");
                    }
                }
            }
            return result.ToString();
        }

        #endregion

        #region Utilities


        private static string ConvertString(object o)
        {
            return o == null ? null : Convert.ToString(o);
        }

        private static int ConvertInt(object o)
        {
            return new ConvertNullable.ConvertNullable().ConvertNullInt(o) ?? 0;
        }

        public static string LeftUrlWithPrefixWithHttp
        {
            get { return HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + SubSystem; }
        }

        public static string LeftUrlWithPrefix
        {
            get { return HttpContext.Current.Request.Url.Host + SubSystem; }
        }

        public static string ServerName
        {
            get
            {
                return HttpContext.Current.Request.Url.Host +
                       (HttpContext.Current.Request.Url.IsDefaultPort ? "" : ":" + HttpContext.Current.Request.Url.Port);
            }
        }

        public static string SubSystem
        {
            get
            {
                var strServerName = ConfigurationSettings.AppSettings["SubSystem"].ToString().Length > 0
                    ? "/" + ConfigurationSettings.AppSettings["SubSystem"].ToString()
                    : "";

                return strServerName;
            }
        }

        #endregion
    }

    public enum SessionLogin
    {
        EmpSeq,
        UserName,
        UserSeq,
        PosSeq,
        WorkingJobSiteSeq,
        WorkingJobSiteName,
        WorkAreaSeq,
        Height,
        TokenKey,
        TokenUserTempData
    }

}